/**
 * @ProjectName: ShopSytem
 * @Title: GoodDaoImpl.java 
 * @Package com.element5th.shop.dao.good 
 * @Description: 前台用户商品相关数据库操作
 * @author 第五元素小组   
 * @date 2012-10-26 15:13:49 
 * @version V1.0   
 */
package com.element5th.shop.dao.good;

import java.util.ArrayList;
import java.util.List;

import com.element5th.shop.comm.BaseDao;
import com.element5th.shop.dto.BaseDto;
import com.element5th.shop.dto.GoodFindDto;
import com.element5th.shop.entity.CartList;
import com.element5th.shop.entity.GoodBigclass;
import com.element5th.shop.entity.GoodClass;
import com.element5th.shop.entity.GoodDetail;
import com.element5th.shop.entity.ShopGood;

/**
 * The Class GoodDaoImpl.
 */
public class GoodDaoImpl extends BaseDao implements IGoodDao {
	/*
	 * Title: getGoodList Description: 得到商品列表
	 * 
	 * @param findDto 商品搜索条件及排序信息
	 * 
	 * @return 查找到的商品列表
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getGoodList(com.element5th.shop
	 * .dto.GoodFindDto)
	 */
	@Override
	public List<BaseDto> getGoodList(GoodFindDto findDto) {
		List<BaseDto> list = new ArrayList<BaseDto>();
		List<ShopGood> goodList = new ArrayList<ShopGood>();
		this.openConnection();

		goodList = this.findAll(ShopGood.class);

		for (ShopGood shopGood : goodList) {
			BaseDto dto = new BaseDto();
			dto.setShopGood(shopGood);
			list.add(dto);
		}
		this.commit();
		return list;
	}

	/*
	 * Title: getGoodListByClass Description: 根据小类得到商品列表
	 * 
	 * @param findDto 商品搜索条件
	 * 
	 * @return 查找到的商品列表
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getGoodListByClass(com.element5th
	 * .shop .dto.GoodFindDto)
	 */
	@Override
	public List<BaseDto> getGoodListByClass(GoodFindDto findDto) {
		List<BaseDto> dtoList = new ArrayList<BaseDto>();
		List<ShopGood> goodList = new ArrayList<ShopGood>();
		this.openConnection();

		goodList = this.findListByCol(ShopGood.class, "classId",
				findDto.getClassId());

		for (ShopGood shopGood : goodList) {
			BaseDto dto = new BaseDto();
			dto.setShopGood(shopGood);
			dto.setGoodDetail(this.findByCol(GoodDetail.class,
					"goodId", shopGood.getGoodId()));
			dtoList.add(dto);
		}
		this.commit();
		return dtoList;
	}

	/*
	 * Title: getGoodDetail Description: 得到商品详细信息
	 * 
	 * @param dto 要查找的商品
	 * 
	 * @return 包含图片列表的商品详细信息对象
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getGoodDetail(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public BaseDto getGoodDetail(BaseDto dto) {
		this.openConnection();
		dto.setGoodDetail(this.findById(GoodDetail.class, dto.getGoodDetail()
				.getGoodId()));
		this.commit();
		return dto;
	}
	
	/*
	 * Title: getGoodDetail Description: 得到商品信息
	 * 
	 * @param dto 要查找的商品
	 * 
	 * @return 包含商品信息的对象
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getGoodDetail(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public BaseDto getGoodInfo(BaseDto dto) {
		this.openConnection();
		dto.setShopGood(this.findById(ShopGood.class,dto.getShopGood().getGoodId()));
		this.commit();
		return dto;
	}


	/*
	 * Title: saveCartList Description: 在用户退出时，保存用户购物车
	 * 
	 * @param dto 包含购物车列表的用户信息对象
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#saveCartList(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public void saveCartList(BaseDto dto) {
		// TODO Auto-generated method stub

	}

	/*
	 * Title: getTotalMoney Description: 获得用户当前购物车商品的总价
	 * 
	 * @param dto 包含购物车列表的用户对象
	 * 
	 * @return 商品总价
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getTotalMoney(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public double getTotalMoney(BaseDto dto) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * Title: goodClassList
	 * 
	 * Description: 获得商品大类包含的种类
	 * 
	 * @param dto 商品大类对象
	 * 
	 * @return 包含种类列表的商品大类对象
	 * 
	 * @see com.element5th.shop.dao.good.IGoodDao#goodClassList()
	 */
	@Override
	public List<BaseDto> goodClassList() {

		List<GoodBigclass> bigClassList = new ArrayList<GoodBigclass>();

		List<GoodClass> classList = new ArrayList<GoodClass>();

		List<BaseDto> dtoList = new ArrayList<BaseDto>();

		this.openConnection();

		bigClassList = this.findAll(GoodBigclass.class);

		for (int i = 0; i < bigClassList.size(); i++) {

			classList = this.findListByCol(GoodClass.class, "bigclassId",
					bigClassList.get(i).getBigclassId());
			bigClassList.get(i).setGoodClasses(classList);
			BaseDto dto = new BaseDto();
			dto.setGoodBigClass(bigClassList.get(i));
			dtoList.add(dto);
		}
		this.commit();
		return dtoList;
	}

	/*
	 * Title: GetClassListByBigClassId
	 * 
	 * Description: 获得商品大类包含的种类
	 * 
	 * @param dto 商品大类对象
	 * 
	 * @return 包含种类列表的商品大类对象
	 * 
	 * @see com.element5th.shop.dao.good.IGoodDao#GetClassListByBigClassId(
	 * com.element5th.shop .dto.BaseDto)
	 */
	@Override
	public List<BaseDto> GetClassListByBigClassId(BaseDto dto) {

		List<GoodClass> classList = new ArrayList<GoodClass>();

		List<BaseDto> dtoList = new ArrayList<BaseDto>();

		GoodBigclass bigclass = new GoodBigclass();

		this.openConnection();

		bigclass = this.findByCol(GoodBigclass.class, "bigclassName", dto
				.getGoodBigClass().getBigclassName());

		classList = this.findListByCol(GoodClass.class, "bigclassId",
				bigclass.getBigclassId());

		for (int i = 0; i < classList.size(); i++) {
			BaseDto baseDto = new BaseDto();
			baseDto.setGoodClass(classList.get(i));
			dtoList.add(baseDto);
		}
		this.commit();
		return dtoList;
	}

	/*
	 * Title: getRecommendGood
	 * 
	 * Description: 获得推荐商品
	 * 
	 * @return 包含推荐商品对象
	 * 
	 * @see com.element5th.shop.dao.good.IGoodDao#getRecommendGood()
	 */
	@Override
	public List<BaseDto> getRecommendGood() {

		this.openConnection();

		List<BaseDto> dtoList = new ArrayList<BaseDto>();

		StringBuffer sql = new StringBuffer(
				"select * from (select *  from shop_good order by Good_SoldNum desc) where rownum<=5");

		List<ShopGood> shopGoods = this.findListBySql(ShopGood.class,
				sql.toString());

		for (int i = 0; i < shopGoods.size(); i++) {
			BaseDto dto = new BaseDto();
			dto.setShopGood(shopGoods.get(i));
			dto.setGoodDetail(this.findByCol(GoodDetail.class, "goodId",
					shopGoods.get(i).getGoodId()));
			dtoList.add(dto);
		}
		this.commit();

		return dtoList;
	}

	/*
	 * Title: getShowGoodList
	 * 
	 * Description: 得到首页商品显示列表
	 * 
	 * @param dto 商品搜索条件
	 * 
	 * @return 查找到的商品列表
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getShowGoodList(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public List<BaseDto> getShowGoodList(BaseDto dto) {

		int count = 0;

		List<ShopGood> goodList = new ArrayList<ShopGood>();

		List<GoodClass> classList = new ArrayList<GoodClass>();

		List<BaseDto> dtoList = new ArrayList<BaseDto>();

		this.openConnection();

		classList = this.findListByCol(GoodClass.class, "bigclassId", dto
				.getGoodBigClass().getBigclassId());

		for (int i = 0; i < classList.size(); i++) {
			goodList = this.findListByCol(ShopGood.class, "classId", classList
					.get(i).getClassId());

			if (count >= 8) {
				break;
			}
			for (int j = 0; j < goodList.size(); j++) {

				BaseDto baseDto = new BaseDto();
				baseDto.setShopGood(goodList.get(j));
				baseDto.setGoodDetail(this.findByCol(GoodDetail.class,
						"goodId", goodList.get(j).getGoodId()));
				dtoList.add(baseDto);
				count++;

			}
		}
		this.commit();
		return dtoList;
	}
	
	/*
	 * Title: getGoodDetail Description: 得到购物车数据信息
	 * 
	 * @param dto 带有用户id的dto
	 * 
	 * @return 购物车商品信息的dto的list
	 * 
	 * @see
	 * com.element5th.shop.dao.good.IGoodDao#getGoodDetail(com.element5th.shop
	 * .dto.BaseDto)
	 */
	@Override
	public List<BaseDto> getCartInfoByUserId(BaseDto dto) {
		this.openConnection();
		ArrayList<BaseDto> list=new ArrayList<BaseDto>();
		ArrayList<CartList> list1=new ArrayList<CartList>();
		//根据用户id查出一个购物车list
		list1=(ArrayList<CartList>) this.findListByCol(CartList.class,"userId",dto.getCartList().getUserId());
		for(int i=0;i<list1.size();i++){
			BaseDto basedto=new BaseDto();
			basedto.setCartList(list1.get(i));
			list.add(basedto);
		}
		
		this.commit();
		return list;
	}
}